public with sharing class Cald_ClaimStatus_Details
{

    public List<Callidus_Claim_Detail__c> claimStatusList {get;set;}
    public List<Callidus_Claim_Detail__c> approvedCommission {get;set;}
    public List<Callidus_Claim_Detail__c> pendingComminssion {get;set;}
    public double sumOfAdjustedCommission{get;set;}
    public double sumOfRequestedCommission {get;set;}
    public List <Opportunity> closedDate ;
    public List <Callidus_Plan_Assignable__c> quotaPlanId;
    public List <Callidus_Plan__c> plantypes ;
    public List <Callidus_Rate__c> ratevalues;
    public List <Callidus_Plan_Participant__c> ytd_credit;
    public double projectedCommissionValue {get;set;}
    
    Private ID oppOwnerId = null;
        
   
    Opportunity o;
    public Cald_ClaimStatus_Details(ApexPages.StandardController mc)
    {
    o =(Opportunity)mc.getrecord();
    opportunity[] ownerList = [select OwnerId from Opportunity where id = :o.id];
    
    oppOwnerId = ownerList[0].OwnerId;
    
    sumOfRequestedCommission =0;
    sumOfAdjustedCommission=0;
    projectedCommissionValue=0.0;
    getApprovedCommissionDetails();
    getPendingCommissionDetails();
    getClaimDetails();
    getProjectedCommissionDetails();
    }

    public void getClaimDetails()
    {
    String oppId=o.id;
    claimStatusList = new  List <Callidus_Claim_Detail__c>();    
    claimStatusList= [select id,Claim__r.Name, Claim__r.Claim_Status__c,Claim__r.Filing_Date__c, Requested_Credit__c, Requested_Commission__c, Requested_Rate__c, Approved_Credit__c, Approved_Commission__c from Callidus_Claim_Detail__c c where c.Claim__r.Claim_Status__c !='Draft' and c.Claim__r.Opportunity__c=:oppId and c.Claimant__c = :oppOwnerId];    
   
    
        if(claimStatusList.size()==0)
        {
            claimStatusList=null;
        }
    }
    
    public void getApprovedCommissionDetails()
    {
    
        String oppId=o.id;
        approvedCommission=new List<Callidus_Claim_Detail__c>();
        approvedCommission=[select Approved_Commission__c from Callidus_Claim_Detail__c c where c.Claimant__c = :oppOwnerId  and  c.Claim__r.Claim_Status__c='Approved' and c.Claim__r.Opportunity__c=:oppId]; 
        if(approvedCommission.size() > 0)
        {
            for(Callidus_Claim_Detail__c P  : approvedCommission)
            {
               sumOfAdjustedCommission= sumOfAdjustedCommission+ P.Approved_Commission__c;
            }
        }
    }
    
    public void getPendingCommissionDetails()
    {
        String oppId=o.id;
        pendingComminssion =new List<Callidus_Claim_Detail__c>();
        pendingComminssion =[select Requested_Commission__c from Callidus_Claim_Detail__c c where c.Claimant__c = :oppOwnerId and  c.Claim__r.Claim_Status__c='Pending Approval' and c.Claim__r.Opportunity__c=:oppId]; 
        if(pendingComminssion.size() > 0)
        {
            for(Callidus_Claim_Detail__c P  : pendingComminssion)
            {
               sumOfRequestedCommission = sumOfRequestedCommission + P.Requested_Commission__c;
            }
        }
    }
    
    
    public void getProjectedCommissionDetails()
    {
        String oppId=o.id;
        //opprtunity variables.
        Date opprtunity_closedate;
        double opprtunity_amount=0.0;

        //plan assignable variables        
        double plann_assignable_quota=0.0;
        ID plann_assignable_planId;
        
        //plan variables
        String plan_type;
        
        //plan participant variables
        //Double YTD_credit_value=0.0;
        double  max_ytdCreditvalue=0.0;
        double  max_YTD_Attainment_value=0.0;
        
        //rate variables
        String rate_type_value;
        Double rate_value=0.0;
        double min_attainment_value=0.0;
        double max_attainment_value=0.0;
        Double min_credit_value=0.0;
        Double max_credit_value=0.0;
        
        double commission=0.0;
                        
        closedDate=new List<Opportunity>();
        quotaPlanId=new List<Callidus_Plan_Assignable__c>();
        plantypes =new List<Callidus_Plan__c>();
        ratevalues=new List<Callidus_Rate__c>();
        ytd_credit=new List<Callidus_Plan_Participant__c>();
        
        //Retrieve close date, amount from an Opportunity.
        closedDate=[Select CloseDate,Amount,ownerid from Opportunity where ownerid=:oppOwnerId and id=:oppId];
        
        if(closedDate.size()==0)
        {
             //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'closedDate is Zero::'+ closedDate));
        }
        
        
        if(closedDate.size()>0)
        {
        for(Opportunity cldate: closedDate)
            {
                 opprtunity_closedate=cldate.CloseDate;
                 opprtunity_amount=cldate.Amount;
                 //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'opprtunity_closedate::'+ opprtunity_closedate));
                 //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'opprtunity_amount::'+ opprtunity_amount)); 
                  
            }
        }    
        
        //Retrieve planid quota from Callidus_Plan_Assignable__c.     
        quotaPlanId=[Select Plan__c,Quota__c from Callidus_Plan_Assignable__c where Participant__c= :oppOwnerId and Effective_Start_Date__c <= :opprtunity_closedate and Effective_End_Date__c >= :opprtunity_closedate];
        
        if(quotaPlanId.size()==0)
        {
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'quotaPlanId is Zero::'+ quotaPlanId));
        }
            
        if(quotaPlanId.size()>0)
        {
            for(Callidus_Plan_Assignable__c quotaplan : quotaPlanId)
            {
                    plann_assignable_quota=quotaplan.Quota__c;
                    plann_assignable_planId=quotaplan.Plan__c;
                    //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'plann_assignable_quota::'+ plann_assignable_quota));
                    //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'plann_assignable_planId::'+ plann_assignable_planId)); 
            }
         }
         
         ytd_credit=[select YTD_Credit__c, YTD_Attainment__c from Callidus_Plan_Participant__c where participant__c=:oppOwnerId and Plan__c=:plann_assignable_planId];
       //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'ytd_credit::'+ ytd_credit)); 
        
        //NEED TO CALICULATE Max YTD_CREDIT and Max_Ytd_commission VALUE. 
        
        if(ytd_credit.size()==0)
        {
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'Ytd credit size is zero::'+ ytd_credit)); 
            projectedCommissionValue=commission;
        }
        
        if(ytd_credit.size()> 0)
        {
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'Ytd credit size is grater than zero::'+ ytd_credit)); 
        
          /* for(Callidus_Plan_Participant__c ytd_cred : ytd_credit)
             {
                   YTD_credit_value=ytd_cred.YTD_Credit__c;
             } */
             
             
             //Finding maximum YTD_Credit value
              
             for(integer i=0;i< ytd_credit.size();i++)
             {
                  max_ytdCreditvalue=ytd_credit[0].YTD_Credit__c;
                  for(integer j=0; j< ytd_credit.size(); j++)
                  {
                      if(ytd_credit[i].YTD_Credit__c > max_ytdCreditvalue)
                      {
                          max_ytdCreditvalue=ytd_credit[i].YTD_Credit__c;
                       //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'max_ytdCreditvalue::'+  max_ytdCreditvalue)); 
                      }
                  }
                  
             }
             
              //Finding maximum YTD_Attainment_value 
              
              for(integer i=0;i< ytd_credit.size();i++)
             {
                   max_YTD_Attainment_value=ytd_credit[0].YTD_Attainment__c;
                  for(integer j=0; j< ytd_credit.size(); j++)
                  {
                      if(ytd_credit[i].YTD_Attainment__c > max_YTD_Attainment_value)
                      {
                          max_YTD_Attainment_value=ytd_credit[i].YTD_Attainment__c;
                        //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'max_YTD_Attainment_value::'+ max_YTD_Attainment_value )); 
                      }
                  }
                  
             }
         }  
        
         
         //Retrieve plan type from Plan__c using planid(plann_assignable_planId)
         
         plantypes =[select Type__c from Callidus_Plan__c where id= :plann_assignable_planId];
         
         if(plantypes.size()==0)
         {
             //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'plantypes is zero::'+ plantypes)); 
         }
         
         if(plantypes.size()>0)
         {
             for(Callidus_Plan__c ptype : plantypes)
             {
                 plan_type=ptype.Type__c;
             //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'plan_type::'+ plan_type)); 
             }
         }
         
         //Retrieve Rate object  based upon planid.
         
         ratevalues=[select Rate__c,Rate_Type__c,Max_Attainment__c,Min_Attainment__c,Max_Credit__c,Min_Credit__c from Callidus_Rate__c where Plan__c=:plann_assignable_planId];
         
         if(ratevalues.size()>0)
         {
             for(Callidus_Rate__c rc: ratevalues)
             {
                rate_type_value=(String)rc.Rate_Type__c;
                rate_value=(double)rc.Rate__c;
                min_attainment_value=(double)rc.Min_Attainment__c;
                max_attainment_value=(double)rc.Max_Attainment__c;                
                max_credit_value=(Double)rc.Max_Credit__c;
                min_credit_value=(Double)rc.Min_Credit__c;
             }
          }
          //code for type=attainment
          if(rate_type_value == 'Attainment')
          {
               double temp_ratevalue=0.0;
               
               if(max_YTD_Attainment_value > 0)
               {
                       for(Callidus_Rate__c rc: ratevalues)
                     {
                         if(max_YTD_Attainment_value>=rc.Min_Attainment__c && max_YTD_Attainment_value< rc.Max_Attainment__c)
                         {
                           temp_ratevalue=(double)rc.Rate__c;
                         }
                        
                     }
                     commission=temp_ratevalue * opprtunity_amount/100;
                     projectedCommissionValue=commission;
               } 
               else
               {
                   projectedCommissionValue=commission;
               }
                 
           }
          //code for type=credit
          if( rate_type_value== 'Credit')
          {
               double temp_ratevalue=0.0;
               
               if(max_ytdCreditvalue>0)
               {
                       for(Callidus_Rate__c rc: ratevalues)
                     {
                         if(max_ytdCreditvalue>=rc.Min_Credit__c && max_ytdCreditvalue< rc.Max_Credit__c)
                         {
                           temp_ratevalue=(double)rc.Rate__c;
                         }
                     }
                     commission=temp_ratevalue * opprtunity_amount/100;
                     projectedCommissionValue=commission;
               }
               
               else
               {
                   projectedCommissionValue=commission;
               }
               
           }
        }// end method getProjectedCommissionDetails()
        
 }